package com.smartStatus.security.comment;

import com.smartStatus.security.handler.RtErrorHandler;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * @ClassName : StatusResourceServerAutoConfig
 * @Author : lby
 * @Date: 2021/5/6 21:05
 * @Description : 全局资源注入
 */
@ComponentScan("com.smartStatus.security")
public class StatusResourceServerAutoConfig {
    //    @Bean
//    @Primary
//    @LoadBalanced
//    public RestTemplate lbRestTemplate() {
//        RestTemplate restTemplate = new RestTemplate();
//        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
//            @Override
//            public void handleError(ClientHttpResponse response) throws IOException {
//                if (response.getRawStatusCode() != HttpStatus.BAD_REQUEST.value()) {
//                    super.handleError(response);
//                }
//            }
//        });
//        return restTemplate;
//    }
    @Bean
    @Primary
    @LoadBalanced
    public RestTemplate lbRestTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setOutputStreaming(false);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        restTemplate.setErrorHandler(new RtErrorHandler());

        return restTemplate;
    }
}
